import React, { ReactNode, useContext } from "react";

const AuthContext = React.createContext<
  | {
      name: "chenjiang";
      age: 24;
    }
  | undefined
>(undefined);
AuthContext.displayName = "AppContext";

export const AuthProvider = ({ children }: { children: ReactNode }) => {
  // AppProvider.tsx文件中传递了children, 这里需要接收
  return (
    <AuthContext.Provider
      value={{
        name: "chenjiang",
        age: 24,
      }}
    >
      {children}
    </AuthContext.Provider>
  );
};

export const useAuth = () => {
  const context = useContext(AuthContext);
  if (!context) {
    throw new Error("请在AppProvider包裹的组件中使用");
  }
  return context;
};
